<?php
set_time_limit(0);

include_once (dirname(__FILE__).'/config/database.php');
include_once (dirname(__FILE__).'/class/Log.inc');
include_once (dirname(__FILE__).'/class/Mysql/Exception.inc');
include_once (dirname(__FILE__).'/class/Mysql.inc');

define('LOCK_FILE', dirname(__FILE__).'/lock/selectlogin.lock');
define('LOG_LEVEL', 'ERROR');

$LogFile = dirname(__FILE__).'/log/selectlogin'.date('Y.m.d').'.log';

$Log = new Zw_Log();
$Log->setCurrLogLevel(LOG_LEVEL);
$Log->setLogFile($LogFile);

$handle = fopen(LOCK_FILE, 'w+');
if(!flock($handle, LOCK_EX|LOCK_NB)){
	$Log->write('进程已经启动，请不要重复启动');
	exit();
}

$GmDb = new Zw_Mysql();
$GmDb->setLog($Log);
$GmDb->connect($gmDatabaseHost, $gmDatabaseUsername, $gmDatabasePassword, $gmDatabaseName);

$GameDb = new Zw_Mysql();
$GameDb->setLog($Log);

$row = 10000;
try {
	for($i=1;$i<=106;$i++){
		$Log->write("执行第{$i}次");
		$start = ($i-1) * $row;

		$sql = "
			SELECT a.userId,a.loginDateTime,d.operatorId,d.operatorName from t_data_game_account_login a
			left join t_data_game_account b on a.userId = b.userId
			LEFT JOIN t_data_game_area c on b.gameAreaId = c.gameAreaId
			left join t_data_operator d on c.operatorId = d.operatorId
			limit {$start},{$row}
 		";
		$user = $GmDb->getArray($sql);
		if($user){
			foreach($user as $v){
				$sql = "
					replace into
						`t_data_game_account_login`
					(
						`userId`,
						`loginDateTime`,
						`operatorId`,
						`operatorName`
					)
					value
					(
						'{$v['userId']}',
						'{$v['loginDateTime']}',
						'{$v['operatorId']}',
						'{$v['operatorName']}'
					)
				";
				$GmDb->query($sql);
			}
		}
	}

}

catch(Zw_Mysql_Exception $e) {
	$Log->write('数据库错误', 'ERROR');
}
catch(Exception $e) {
	$Log->write('系统错误', 'ERROR');
}
